package com.xxyf.config;

import com.xxyf.filter.AccessDeniedHandlerImp;
import com.xxyf.filter.AuthenticationImp;
import com.xxyf.filter.LoginFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 小小怡飞
 * @Date 2022/11/20 2:24
 * @Version JDK 8
 */

@EnableWebSecurity
@Slf4j

public class SecurityConfig  {
    @Autowired
    private AccessDeniedHandlerImp accessDeniedHandlerImp;
    @Autowired
    private AuthenticationImp authenticationEntryPoint;
    @Autowired
    private AuthenticationConfiguration authenticationConfiguration;

    @Bean
    public PasswordEncoder PasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private LoginFilter loginFilter;
    @Bean
    public SecurityFilterChain securityWebFilterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/login.do").anonymous()

                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

//        httpSecurity.formLogin().successHandler(new AuthenticationSuccessHandlerImp());

        httpSecurity.addFilterBefore(loginFilter, UsernamePasswordAuthenticationFilter.class);
        httpSecurity.exceptionHandling().accessDeniedHandler(accessDeniedHandlerImp).authenticationEntryPoint(authenticationEntryPoint);
        return  httpSecurity.build();
    }
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
    static class AuthenticationSuccessHandlerImp implements AuthenticationSuccessHandler{
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            System.out.println("认证成功");
            log.debug("sss");
        }
    }
}
